# -*- coding: utf-8 -*-
#coding=utf-8

import config
from action.admin.base import adminAction
from model.product import product
from model.prodcomment import prodcomment
from model.category import category
from model.user import user

'''
Admin controller: producing prodcomment administration views.
'''
class refererAction(adminAction):
    def __init__(self, name = '产品评论管理'):
        adminAction.__init__(self, name)


    def GET(self, name):
        if name == 'productlist':
            return self.productlist()
        if name == 'list':
            return self.list()
        elif name == 'delete':
            return self.delete()
        elif name == 'edit':
            return self.edit()
        else:
            return self.notFound()

    def POST(self, name):
        if name == 'modify':
            return self.modify()
        elif name == 'deleteBatch':
            return self.deleteBatch()
        else:
            return self.notFound()

    def list(self):
        inputParams = self.getInput()
        page = int(inputParams['page']) if inputParams.has_key('page') else 1
        count = config.COUNT_PER_PAGE
        offset= (page-1)*count if page > 0 else 0
   
        condition = {}
        prodcommentObj = prodcomment()
        listData = prodcommentObj.getOne('COUNT(*) AS `total`', condition)
        totalCount = listData['total']
        condition = {'product':inputParams['id']}
        prodcommentList = prodcommentObj.getList('*', condition, 'id desc', str(offset) + ',' + str(count))

        pageString = self.getPageStr(self.makeUrl('/admin/prodcomment/list'), page, count, totalCount)
        self.privData['COMMENT_LIST'] = prodcommentList
        self.privData['PAGE_STRING'] = pageString
        self.privData['PRODUCT_ID'] = inputParams['id']

        return self.display('prodcommentList')

    def productlist(self):
        inputParams = self.getInput()
        page = int(inputParams['page']) if inputParams.has_key('page') else 1
        count = config.COUNT_PER_PAGE
        offset= (page-1)*count if page > 0 else 0
   
        condition = {}
        productObj = product()
        listData = productObj.getOne('COUNT(*) AS `total`', condition)
        totalCount = listData['total']
        producList = productObj.getList('*', condition, 'id desc', str(offset) + ',' + str(count))

        pageString = self.getPageStr(self.makeUrl('/admin/prodcomment/productlist'), page, count, totalCount)
        self.privData['PRODUCT_LIST'] = producList
        self.privData['PAGE_STRING'] = pageString

        categoryList = category().getList('*', condition)
        self.privData['CATEGORY_LIST'] = {}
        for i in categoryList:    
            self.privData['CATEGORY_LIST'][i['id']] = i['name']
        return self.display('procommViewList')

    def delete(self):
        inputParams = self.getInput()
        try:
            condition = {'id': int(inputParams['id'])}
            prodcomment().delete(condition)
        except Exception, e:
            return self.error(msg='评论删除失败: %s' % e, url=self.makeUrl('/admin/prodcomment/list',{'id':inputParams['product']}))

        return self.success(msg='评论删除成功', url=self.makeUrl('/admin/prodcomment/list',{'id':inputParams['product']}))

    def deleteBatch(self):
        inputParams = self.getInput()
        try:
            condition = ' id IN (' + inputParams['delitems'] +')'
            prodcomment().delete(condition)
        except Exception, e:
            return self.error(msg='评论删除失败: %s' % e, url=self.makeUrl('/admin/prodcomment/list'))

        return self.success(msg='评论删除成功', url=self.makeUrl('/admin/prodcomment/list'))

    def modify(self):
        userInput= self.getInput()
        try: 
            data={
                'content':      userInput['content'],
            }

            condition={'id': int(userInput['id'])}
            prodcomment().update(data, condition)
        except Exception, e:
            return self.error(msg = '评论修改失败: %s' % e, url=self.makeUrl('/admin/prodcomment/list',{'id':userInput['product']}))

        return self.success('评论修改成功', url=self.makeUrl('/admin/prodcomment/list',{'id':userInput['product']}))

    def edit(self):
        inputParams = self.getInput()
        condition={'id': int(inputParams['id'])}
        prodcommentObj = prodcomment().getOne('*', condition)
        if not prodcommentObj:
            return self.error(msg = '评论不存在')
        self.privData['prodcomment_INFO'] = prodcommentObj
        condition={'id': int(prodcommentObj['user'])}
        userObj = user().getOne('nickname', condition)
        self.privData['USER'] = userObj['nickname']
        self.privData['PRODUCT_ID'] = inputParams['product']

        return self.display('prodcommentEdit')

